import requests
import pandas as pd
from urllib.parse import urlencode

# 定义API的参数
params = {
    'key': '7eecaf8606c454d7dce4eac9ae699675',
    'location': '114.273801,30.59283',  # 使用提供的经纬度
    'keywords': '快递站网点',
    'types': '070500|070501',
    'radius': 10000,
    'offset': 25,
    'page': 3,
    'extensions': 'all'
}

# 构建请求的URL
url = f"https://restapi.amap.com/v3/place/around?{urlencode(params)}"

# 发送GET请求
response = requests.get(url)

# 检查请求是否成功
if response.status_code == 200:
    # 解析返回的JSON数据
    data = response.json()

    # 检查是否有POI数据
    if data.get('count') != '0' and data.get('pois'):
        # 提取pois数据
        pois = data['pois']

        # 创建DataFrame
        df = pd.DataFrame(pois)

        # 选择需要展示的列
        columns = ['name', 'address', 'distance', 'location']
        df_selected = df[columns]

        # 将DataFrame保存为Excel文件
        df_selected.to_excel("良友快递站网点信息2.xlsx", index=False, engine='openpyxl')
        print("数据已保存到 '良友快递站网点信息2.xlsx'")
    else:
        print("没有找到数据或数据为空。")
else:
    print("请求失败，状态码：", response.status_code)